package com.cms.intelligent_control.instructions

import android.app.Activity
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cms.intelligent_control.R
import com.cms.intelligent_control.config.Config
import com.cms.intelligent_control.common.http.Api
import com.cms.intelligent_control.module.VideoModule
import com.cms.intelligent_control.utils.toList
import io.reactivex.android.schedulers.AndroidSchedulers
import org.json.JSONObject

class AudioVideoCmd(var activity: Activity, private val videoModule: VideoModule) {
    private val mRecyclerView = activity.findViewById<RecyclerView>(R.id.rv_audio)

    private var mAudioVideoList: MutableList<JSONObject> = mutableListOf()
    private var mCmdKeyword = mutableListOf<String>()
    private var mCurrentAudio = -1
    private var isShowAudioVideoList = false

    init {
        mRecyclerView.layoutManager = LinearLayoutManager(activity)
        mRecyclerView.addItemDecoration(
            DividerItemDecoration(
                activity,
                DividerItemDecoration.VERTICAL
            )
        )
    }

    public fun isAudioVideoListAction(data: JSONObject): Boolean {
        val type = data.optString("instruct_type")
        return arrayOf("媒体文件播放").contains(type)
    }

    public fun execute(data: JSONObject) {
        val type = data.optString("instruct_type")
        this.getAudioVideoList()
    }

    public fun executeLocalCmd(msg: String) {
        if (!isShowAudioVideoList) {
            return
        }
        if (videoModule.getPlayStatus() != VideoModule.Status.STATUS_READY) {
            return
        }
        val hitIndex = getHitAudioVideoIndex(msg)
        if (hitIndex != -1) {
            var obj = JSONObject()
            obj.put("instruct_type", "音视频播放")
            obj.put(
                "exe_operation",
                Config.baseFilePrefix + mAudioVideoList[hitIndex].get("file_path")
            )
            videoModule.execute(obj)
            mCurrentAudio = hitIndex
            mRecyclerView.adapter?.notifyDataSetChanged()
        }
    }

    public fun showAudioVideoList(list: MutableList<JSONObject>) {
        this.mAudioVideoList = list
        this.getCmdKeyword(mAudioVideoList)
        val adapter =
            object : BaseQuickAdapter<JSONObject, BaseViewHolder>(R.layout.item_audio, list) {
                override fun convert(holder: BaseViewHolder, item: JSONObject) {
                    holder.setText(
                        R.id.tv_audio,
                        "${holder.layoutPosition + 1}、${item.optString("file_name")}"
                    )
                    val res = if (holder.layoutPosition == mCurrentAudio)
                        R.mipmap.audio_s else R.mipmap.audio_n
                    holder.setImageResource(R.id.iv_icon, res)
                    holder.setVisible(R.id.iv_play, holder.layoutPosition == mCurrentAudio)

                }
            }
        mRecyclerView.adapter = adapter
        mRecyclerView.visibility = View.VISIBLE
        isShowAudioVideoList = true
    }

    public fun hideAudioVideoList() {
        mAudioVideoList.clear()
        mCmdKeyword.clear()
        isShowAudioVideoList = false
        mCurrentAudio = -1
        mRecyclerView.visibility = View.GONE
    }

    public fun onPlayCompletion() {
        mCurrentAudio = -1
        mRecyclerView.adapter?.notifyDataSetChanged()
    }

    private fun getAudioVideoList() {
        val d = Api.getApi().findFileList("1")
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (it.getInt("status") == 0) {
                    var list = it.optJSONArray("data").toList()
                    if (list.size > 5) {
                        list = list.subList(0, 5)
                    }
                    showAudioVideoList(list)
                }
            }, {
                Log.e("AudioVideoCmd", "", it)
            })
    }

    private fun getHitAudioVideoIndex(msg: String): Int {
        for (i in 0 until mCmdKeyword.size) {
            if (msg.contains(Regex(mCmdKeyword[i]))) {
                return i
            }
        }
        return -1
    }

    private fun getCmdKeyword(list: MutableList<JSONObject>) {
        mCmdKeyword.clear()
        for (i in 0 until list.size) {
            mCmdKeyword.add("${list[i].optString("file_name")}|${Constant.defaultKey[i]}")
        }
    }
}